<?php
namespace Modules\ShopApi\Models; 
use Modules\Admin\Services\UserService;
use Illuminate\Support\Facades\DB;
class ShopSite extends BaseApiModel
{	
	/**
     * @name  获取我的收获地址 
     */
	public function get_user_site(){
		$authorization = \Request::header('authorization');	
		if($authorization){
			$user_info = (new UserService())->my();
			if($user_info===false){
				$location = '';
			}else if(isset($user_info['code']) && $user_info['code'] == 1003){
				return $this->api_error($user_info);
			}else{
				
				$info = $this->where(['user_id'=>$user_info['id']])
					->select('id','province_id','city_id','county_id','detailed_address')
					->with([
							'province_one'=>function($query){
								$query->select('id','name');
							},
							'city_one'=>function($query){
								$query->select('id','name');
							},
							'county_one'=>function($query){
								$query->select('id','name');
							}
						])
					->first();
				if($info){
					$location = $info['province_one']['name'] . ',' . $info['city_one']['name'] . ',' . $info['county_one']['name']	. ',' . $info['detailed_address'];
				}else{
					$location = '';
				}	
				
				
			}
		}else{
			$location = '';
		}
		return $this->api_success(['data'=>['location'=>$location]]);
	}
	/**
     * @name  添加地址 
     */
	public function add_site($data){
		$user_info = (new UserService())->my();
		$data['user_id'] = $user_info['id'];
		$data['created_at'] = date('Y-m-d H:i:s');
		DB::beginTransaction();
		try{
			if($data['status']==1){
				$this->where(['user_id'=>$user_info['id']])->update(['status'=>0]);
			}
			$this->insert($data);
		DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();		
	}
	/**
     * @name  关联省
     */
	public function province_one(){
        return $this->hasOne('Modules\ShopApi\Models\Area','id','province_id');
    }
	/**
     * @name  关联市
     */
	public function city_one(){
        return $this->hasOne('Modules\ShopApi\Models\Area','id','city_id');
    }
	/**
     * @name  关联区县
     */
	public function county_one(){
        return $this->hasOne('Modules\ShopApi\Models\Area','id','county_id');
    }
	/**
     * @name  地址列表 
     */
	public function site_list(){
		$user_info = (new UserService())->my();
		$list = $this->where(['user_id'=>$user_info['id']])
			->select('id','name','phone','province_id','city_id','county_id','detailed_address','status')
			->with([
					'province_one'=>function($query){
						$query->select('id','name');
					},
					'city_one'=>function($query){
						$query->select('id','name');
					},
					'county_one'=>function($query){
						$query->select('id','name');
					}
				])
			->get()
			->toArray();
		$list = $this->site_list_data($list);	
		return $this->api_success(['data'=>$list]);		
	}
	/**
     * @name  地址列表数据处理
     */
	private function site_list_data($list){
		foreach($list as $k=>$v){
			$list[$k]['province_name'] = $list[$k]['province_one']['name'];
			$list[$k]['city_name'] = $list[$k]['city_one']['name'];
			$list[$k]['county_name'] = $list[$k]['county_one']['name'];
			unset($list[$k]['county_one'],$list[$k]['city_one'],$list[$k]['province_one'],$list[$k]['province_id'],$list[$k]['city_id'],$list[$k]['county_id']);
		}
		return $list;
	}
	/**
     * @name  地址编辑页面数据
     */
	public function get_list_info($id){
		$info = $this->where(['id'=>$id])
			->select('id','name','phone','province_id','city_id','county_id','detailed_address','status')
			->with([
					'province_one'=>function($query){
						$query->select('id','name');
					},
					'city_one'=>function($query){
						$query->select('id','name');
					},
					'county_one'=>function($query){
						$query->select('id','name');
					}
				])
			->first()
			->toArray();
		$info['province_name'] = $info['province_one']['name'];
		$info['city_name'] = $info['city_one']['name'];
		$info['county_name'] = $info['county_one']['name'];
		unset($info['county_one'],$info['city_one'],$info['province_one']);	
		return $this->api_success(['data'=>$info]);			
	}
	/**
     * @name  地址编辑 
     */
	public function edit_site($data){
		$user_info = (new UserService())->my();
		$id = $data['id'];
		unset($data['id']);
		$data['updated_at'] = date('Y-m-d H:i:s');
		DB::beginTransaction();
		try{
			if($data['status']==1){
				$this->where(['user_id'=>$user_info['id']])->update(['status'=>0]);
			}
			$this->where('id',$id)->update($data);
		DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();		
	}
	/**
     * @name  地址删除 
     */
	public function delete_site($id){
		if($this->destroy($id)){
			return $this->api_success();
		}else{
			return $this->api_error();
		}
		
		
		
			
	}
}
